package com.vueblog.webapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vueblog.webapi.entity.BlogEntity;
import com.vueblog.webapi.entity.BlogTagsEntity;
import com.vueblog.webapi.entity.CommentEntity;
import com.vueblog.webapi.entity.UserEntity;
import com.vueblog.webapi.enums.ResponseEnum;
import com.vueblog.webapi.mapper.CommentMapper;
import com.vueblog.webapi.mapper.UserMapper;
import com.vueblog.webapi.request.CreateCommentInfoRequest;
import com.vueblog.webapi.response.BaseResponse;
import com.vueblog.webapi.response.CommentsInfoResponse;
import com.vueblog.webapi.service.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vueblog.webapi.service.IUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author luosj
 * @since 2021-05-15
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, CommentEntity> implements ICommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserMapper userMapper;

    //访客图片
    @Value("${comment.avatar}")
    private String avatar;

    /**
     * 根据博客id获评论信息
     */
    @Override
    public BaseResponse getCommentsByBlogId(Long id) {
        // 获取所有的父级评论
        List<CommentEntity> parentComments = commentMapper.selectList(new QueryWrapper<CommentEntity>().lambda()
                .eq(CommentEntity::getBlogId, id)
                .isNull(CommentEntity::getParentCommentId));
        // 初始化封装要返回的评论集
        List<CommentsInfoResponse> commentsInfoResponseList = new ArrayList<>();
        if (parentComments.size() != 0) {
            for (CommentEntity parentComment : parentComments) {
                // 根据父级评论的id查询子级评论集
                List<CommentEntity> chidrenComments = commentMapper.selectList(new QueryWrapper<CommentEntity>().lambda()
                        .eq(CommentEntity::getParentCommentId, parentComment.getId()));
                // 子级评论集根据创建时间升序排序
                chidrenComments.sort((x, y) -> LocalDateTime.now().compareTo(x.getCreateTime()));
                // 封装父级评论以及对应的子级评论
                CommentsInfoResponse commentsInfoResponse = new CommentsInfoResponse(parentComment, chidrenComments);
                // 遍历添加要返回的评论
                commentsInfoResponseList.add(commentsInfoResponse);
            }
        }
        // 返回的评论集根据父级评论的创建时间降序排序
        commentsInfoResponseList.sort((x, y) -> -LocalDateTime.now().compareTo(x.getParentComment().getCreateTime()));
        return new BaseResponse(ResponseEnum.SUCCESS, commentsInfoResponseList);
    }

    /**
     * 创建评论
     */
    @Override
    public BaseResponse createComment(CreateCommentInfoRequest request) {
        CommentEntity comment = new CommentEntity();
        comment
                .setBlogId(request.getBlogId())
                .setNickname(request.getNickname())
                .setReplyNickname(request.getReplyNickname())
                .setEmail(request.getEmail())
                .setContent(request.getContent())
                .setCreateTime(LocalDateTime.now());
        if (request.getParentCommentId() == -1) {
            comment.setParentCommentId(null);
        } else {
            comment.setParentCommentId(request.getParentCommentId());
        }
        // 获取当前的用户
//        Subject subject = SecurityUtils.getSubject();
//        UserEntity user =(UserEntity) subject.getPrincipal();
        UserEntity user = userMapper.selectOne(new QueryWrapper<UserEntity>().lambda()
                .eq(UserEntity::getIsOnLine, true));
        if (user == null) {
            comment
                    .setAdminComment(false)
                    .setAvatar(avatar);
        } else {
            comment
                    .setAdminComment(true)
                    .setAvatar(user.getAvatar());
        }
        commentMapper.insert(comment);
        return new BaseResponse(ResponseEnum.SUCCESS);
    }

    @Transactional
    @Override
    public BaseResponse deleteComment(String id) {
        // 先删除子评论
        commentMapper.delete(new QueryWrapper<CommentEntity>().lambda()
                .eq(CommentEntity::getParentCommentId, id));
        // 再删除父级评论
        int result = commentMapper.deleteById(id);
        return new BaseResponse(ResponseEnum.SUCCESS);
    }


}
